<?php
/**
 * 导入文件读取函数
 * @param 传入的文件名称 $filename
 * @return string
 */
function getCsvFileContent($filename)
{
    $row = 1;//第一行开始
    if (($handle = fopen($filename, 'r')) !== false) {
        while (($dataSrc = fgetcsv($handle)) !== false) {
            $num = count($dataSrc);
            for ($c=0; $c < $num; $c++) {
                if($row === 1) {
                    $dataName[] = mb_convert_encoding($dataSrc[$c], 'UTF-8', 'GBK');//字段名称
                } else {
                    foreach ($dataName as $k=>$v) {
                        if($k == $c) {
                            $data[$v] = mb_convert_encoding($dataSrc[$c], 'UTF-8', 'GBK');
                        }
                    }
                }
            }
            if (!empty($data)) {
                $csvFileContent[] = $data;
                unset($data);
            }
            $row++;
        }
        fclose($handle);
        return $csvFileContent;
    }
}

function getProfitName($bit_pay)
{
    switch ($bit_pay) {
        case UTID_PROVIDER:
            return "供货商货款";
        case UTID_PROSECOND:
            return "二级供应商分成";
        case UTID_PURCHASER:
            return "采购商分成";
        case UTID_BEIZHU:
            return "着想分成";
        case UTID_MANAGER_L1:
            return "一级代理商分成";
        case UTID_MANAGER_L2:
            return "二级代理商分成";
        case UTID_GROWTH:
            return "发展商分成";
        case UTID_SELLER:
            return "分销点分成";
        case 8192:
            return "实际支付扣款";
        case 16384:
            return "消费者积分";
        case 65536:
            return "商品已付款";
        case 131072:
            return "交易已处理";
        case 262144:
            return "账户提现";
        case 524288:
            return "账户预充值";
        case 1048576:
            return "商品已结算";
        case 32768:
            return "积分奖励";
        case 2097152:
            return "冻结金额";
        case 8388608:
            return "提现失败";
        case 4194304:
            return "提现成功";
        case 33554432:
            return "获得金币";
        case 67108864:
            return "消耗金币";
        case 134217728:
            return "使用金币";
        default:
            return "未知的[$bit_pay]";
    }
}

/**
 * 只保留字符串首尾字符，隐藏中间用*代替（两个字符时只显示第一个）
 * @param string $user_name 姓名
 * @return string 格式化后的姓名
 */
function substr_cut($user_name)
{
    $strlen = mb_strlen($user_name, 'utf-8');
    if ($strlen == 0 || $strlen == 1) {
        return $user_name;
    }
    if (preg_match('/^1[34578]\d{9}$/', $user_name)) { //如果是手机号码
        return subStrMobilePhone($user_name);
    }
    $firstStr = mb_substr($user_name, 0, 1, 'utf-8');
    $lastStr  = mb_substr($user_name, -1, 1, 'utf-8');
    return $strlen == 2 ? $firstStr . '*' : $firstStr . str_repeat('*', $strlen - 2) . $lastStr;
}

/**
 * 身份证字符，隐藏中间用*代替（不是18位或15位的直接返回）
 * @param unknown $sfz 身份证
 * @return mixed
 */
function substr_sfz($sfz)
{
    $strlen = strlen($sfz);
    switch ($strlen) {
        case 18 :
            $sfz = substr_replace($sfz,'********', 6, 8);
            break;
        case 15 :
            $sfz = substr_replace($sfz,'****', 8, 4);
            break;
        default:
            break;
    }
    return $sfz;
}

/**
 * 手机号码，中间四位用*代替
 * @param $phone
 * @return mixed|string
 */
function subStrMobilePhone($phone)
{
    $length = strlen($phone);
    if ( $length != 11 ) {
        return '';
    }
    return substr_replace($phone, '****', 3, 4);
}

/**
 * 对固定电话或传真进行拆分。
 * @param unknown $telephone 固定电话或传真
 * @param string $position 为true返回区号，默认返回区号后号码
 * @return unknown|string
 */
function subStrTelephone($telephone, $position=false)
{
    if (empty($telephone)) {
        return $telephone;
    }
    if (strpos($telephone, '-') !== false) {
        return $position ? strstr($telephone, '-', true) : ltrim(strstr($telephone, '-'), '-');//返回前半部分
    }
    if (preg_match('/^(010|021|022|023).*$/', $telephone)) {//如果前三位出现直辖市区号(010|021|022|023)，则返回直辖市电话号码
        return $position ? substr($telephone, 0, 2) : substr($telephone, -3);
    } else {
        return $position ? substr($telephone, 0, 3) : substr($telephone, -4);
    }
}

/**
 * 截取邮箱  显示邮箱第一位 和最后一位
 * @param $email
 * @return bool|string
 */
function subStrEmail($email)
{
    if (empty($email)) {
        return $email;
    }
    $tail = strstr($email, '@');
    $email = substr($email, 0, strpos($email, '@'));
    return substr($email, 0, 1). '*****'. substr($email, -1, 1). $tail;
}

//用户类型中文显示
function getTypeChinese($type)
{
    switch ($type) {
        case 'provider':
            return '供应商';
        case 'purchaser':
            return '采购商';
        case 'manager_l1':
            return '省级代理';
        case 'manager_l2':
            return '市级代理';
        case 'growth':
            return '分销商';
        case 'seller':
            return '分销商';
        case 'customer':
            return '消费者';
        case 'teller':
            return '检票员';
        default:
            return '未知用户类型';
    }
}

//得到账户名称由账户ID
function getAccountNameByAccountID($account_type)
{
    switch ($account_type) {
        case ACCOUNT_TYPE_CARRY:
            return 'amount_carry';
        case ACCOUNT_TYPE_SETTLEMENT:
            return 'amount_settlement';
        case ACCOUNT_TYPE_BONUS:
            return 'amount_bonus';
        case ACCOUNT_TYPE_RANK:
            return 'rank';
        case ACCOUNT_TYPE_MONTH:
            return 'amount_month';
        default:
            return NULL;
    }
}

//过滤微信表情
function filterEmoji($str)
{
    $str = preg_replace_callback(
        '/./u',
        function ($match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str
    );

    return $str;
}

/**
 * Validate mobile phone
 * @param unknown $mobile
 * @return boolean
 */
function valid_mobile($mobile)
{
    return (preg_match('/^1[23456789]\d{9}$/', $mobile)) ? TRUE : FALSE;
}

/**
 * 1，2转换成大写中文数字
 * @param $number
 * @return string
 */
function numberToZhUpper($number)
{
    $num = '';
    switch($number){
        case 1 :
            $num = '一';
            break;
        case 2 :
            $num = '二';
            break;
        case 3 :
            $num = '三';
            break;
        case 4 :
            $num = '四';
            break;
        case 5 :
            $num = '五';
            break;
        case 6 :
            $num = '六';
            break;
        case 7 :
            $num = '七';
            break;
        case 8 :
            $num = '八';
            break;
        case 9 :
            $num = '九';
            break;
        case 10 :
            $num = '十';
            break;
        default:
            break;
    }
    return $num;
}

/**
 * 获取ip地址
 * @return string
 */
function getCip()
{
    //php获取ip的算法
    if (isset($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) )
    {
        $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
    } elseif (isset($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]))
    {
        $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
    }
    elseif (isset($HTTP_SERVER_VARS["REMOTE_ADDR"]))
    {
        $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
    }
    elseif (getenv("HTTP_X_FORWARDED_FOR"))
    {
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    }
    elseif (getenv("HTTP_CLIENT_IP"))
    {
        $ip = getenv("HTTP_CLIENT_IP");
    }
    elseif (getenv("REMOTE_ADDR"))
    {
        $ip = getenv("REMOTE_ADDR");
    }
    else
    {
        $ip = "Unknown";
    }
    return ip2long($ip);
}

/**
 * 删除过期文件
 * @param  [type]  $filePath   文件路径
 * @param  array  $format     文件格式
 * @param  integer $expiration 过期日期
 * @return [type]              [description]
 */
function deleteExpiredFiles($filePath, $format = '.png', $expiration = 7200)
{
    $filePath = realpath($filePath);
    $current_dir = @opendir($filePath);
    $now = time();
    $len = strlen($format);
    while ($filename = @readdir($current_dir)) {
        if (substr($filename, -$len) === $format && is_file($filePath. DIRECTORY_SEPARATOR .$filename)) {
            if ((filectime($filePath. DIRECTORY_SEPARATOR .$filename) + $expiration) < $now) {
                @unlink($filePath. DIRECTORY_SEPARATOR. $filename);
            }
        }
    }
    @closedir($current_dir);
}

/**
 * 线路产品名称展示
 * @param bool $type
 * @return array
 */
function lineProductType($type = false)
{
    $CI = & get_instance();
    $CI->load->model('line_product_type_model', 'line_product_type');
    $result = $CI->line_product_type->find();
    if ($type) {
        $rows = array();
        foreach ($result->result_array() as $row) {
            $rows[$row['id']] = $row;
        }
        return $rows;
    }
    return $result->result();
}

/**
 * 线路支付类型
 * @param bool $type
 * @return array
 */
function linePayStyleTypeArray($type = false)
{
    $CI = & get_instance();
    $CI->load->model('pay_style_model', 'pay_style');
    $result = $CI->pay_style->find();
    if ($type) {
        $rows = array();
        foreach ($result->result_array() as $row) {
            $rows[$row['id']] = $row;
        }
        return $rows;
    }
    return $result->result();
}

/**
 * 线路往返交通展示
 * @param bool $type
 * @return array
 */
function lineTraffic($type = false)
{
    $CI = & get_instance();
    $CI->load->model('line_traffic_model', 'line_traffic');
    $result = $CI->line_traffic->find();
    if ($type) {
        $rows = array();
        foreach ($result->result_array() as $row) {
            $rows[$row['id']] = $row;
        }
        return $rows;
    }
    return $result->result();
}

/**
 * 线路类型展示
 * @param bool $type
 * @return array
 */
function lineTravelType($type = false)
{
    $CI = & get_instance();
    $CI->load->model('line_travel_type_model', 'line_travel_type');
    $result = $CI->line_travel_type->find();
    if ($type) {
        $rows = array();
        foreach ($result->result_array() as $row) {
            $rows[$row['id']] = $row['name'];
        }
        return $rows;
    }
    return $result->result();
}


/**
 * 线路类型ID展示
 * @return array
 */
function lineTravelTypeId()
{
    $CI = & get_instance();
    $CI->load->model('line_travel_type_model', 'line_travel_type');
    $result = $CI->line_travel_type->find();
    $rows = array();
    foreach ($result->result_array() as $row) {
        $rows[] = $row['id'];
    }
    return $rows;
}

/**
 *  线路价格区间的展示
 *  @return array
 */
function priceRangeArray()
{
    return array(
        '1'=>'0-100',
        '2'=>'101-300',
        '3'=>'301-800',
        '4'=>'801-2000',
        '5'=>'2001-6000',
        '6'=>'6001-10000',
        '7'=>'10000以上'
    );
}
 
function lineOrderStatusArray(){
    return array(
        '1'=>'待确认订单',
        '2'=>'已确认订单',
        '3'=>'已取消订单',
        '4'=>'已付款订单',
        '5'=>'已完成订单',
        '6'=>'已退款订单',
        '7'=>'过期订单'
    );
}

/**
 * 线路支付状态
 * @return array
 */
function lineOrderStateArray()
{
    return array(
        '1'=>'未支付',
        '2'=>'已支付',
        '3'=>'已完成',
        '4'=>'已退款'
    );
}

function getAccountAmount($account_type,$item)
{
    switch($account_type)
    {
        case "amount_carry":
            return $item->amount_carry;
        case "amount_settlement":
            return $item->amount_settlement;
        case "amount_bonus":
            return $item->amount_bonus;
        case "rank":
            return $item->rank;
        case "amount_month":
            return $item->amount_month;
        case 'outlay_total':
            return $item->outlay_total;
        case 'income_total':
            return $item->income_total;
        default:
            return NULL;
    }
}

/**
 * 快递状态
 * @return array
 */
function expressState()
{
    return [
        '-1'=> '快递公司未收录',
        '0' => '在途中',
        '1' => '已揽收',
        '2' => '疑难',
        '3' => '已签收',
        '4' => '退签',
        '5' => '同城派送中',
        '6' => '退回',
        '7' => '转单'
    ];
}

/**
 * 快递状态
 * @return array
 */
function expressIsCheck()
{
    return [
        '-1'=> '快递公司未收录',
        '0' => '在途',
        '1' => '揽件',
        '2' => '疑难',
        '3' => '签收成功'
    ];
}

/**
 * 抽奖奖品等级转换
 * @return array
 */
function prizeGrade()
{
    return array(
        1 => '一',
        2 => '二',
        3 => '三',
        4 => '四',
        5 => '五',
        6 => '六',
        7 => '七',
        8 => '八',
        9 => '九'
    );
}